import json

import requests
import re
from urllib3.exceptions import InsecureRequestWarning


def get_all_stock():
    """获取所有股票的实时数据"""
    data = {}
    timeout = 5  # timeout修改了超时时间，以秒为单位
    # 构造URL请求、user-agent头文件
    url = 'http://49.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124004803065610352708_1670680410619&pn=1&pz=9999' \
          '&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,' \
          'm:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,' \
          'f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152,f26,f27,f28,f29,f30,f31,f32,f33,f34,f35,f36,f37,f38,' \
          'f39,f40,f41,f42,f43,f44,f45,f46,f47,f48,f49'
    headers = {
        'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 5.1.1; TAS-AN00 Build/TAS-AN00)'}

    session = requests.Session()
    # 禁用安全请求警告
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    # dicts = {}
    try:
        html = session.post(url=url, data=data, headers=headers, verify=False, timeout=timeout).text
        str = re.sub('\w+\(', '', html, 0, 0)
        str = re.sub('\);', '', str, 0, 0)
        html = json.loads(str)
    except Exception as spider_error:
        raise ValueError(spider_error)
    return html


def get_min_line_data_eastmoney(code, days='2'):
    """获取股票的涨幅等数据"""
    data = {}
    timeout = 5  # timeout修改了超时时间，以秒为单位
    # 构造URL请求、user-agent头文件
    url = 'http://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,' \
          'f13&fields2=f51,f52,f53,f54,f55,f56,f57,' \
          'f58&ut=fa5fd1943c7b386f172d6893dbfba10b&secid=' + code + '&ndays=' + days + '&iscr=0&iscca=0&cb=jsonp1670770705543 '
    headers = {
        'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 5.1.1; TAS-AN00 Build/TAS-AN00)'}

    session = requests.Session()
    # 禁用安全请求警告
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    # dicts = {}
    try:
        html = session.get(url=url, data=data, headers=headers, verify=False, timeout=timeout).text
        _str = re.search('\{(\S*\s*)*\}', html).group()
        html = json.loads(_str)
    except Exception as spider_error:
        raise ValueError(spider_error)
    return html


def get_yesterday_zt_pool(date):
    """昨日涨停股票池"""
    data = {}
    timeout = 5  # timeout修改了超时时间，以秒为单位
    # 构造URL请求、user-agent头文件
    url = 'http://push2ex.eastmoney.com/getYesterdayZTPool?cb=callbackdata1645805&ut=7eea3edcaed734bea9cbfc24409ed989' \
          '&dpt=wz.ztzt&Pageindex=0&pagesize=999999&sort=zs%3Adesc&date=' + date
    headers = {
        'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 5.1.1; TAS-AN00 Build/TAS-AN00)'}

    session = requests.Session()
    # 禁用安全请求警告
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    # dicts = {}
    try:
        html = session.get(url=url, data=data, headers=headers, verify=False, timeout=timeout).text
        _str = re.search('\{(\S*\s*)*\}', html).group()
        html = json.loads(_str)
    except Exception as spider_error:
        raise ValueError(spider_error)
    return html


def get_history_date_eastmoney():
    """历史日期"""
    import time
    m_time = time.time()
    m_time = str(round(m_time*1000))
    data = {}
    timeout = 5  # timeout修改了超时时间，以秒为单位
    # 构造URL请求、user-agent头文件
    # url = 'https://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get?cb=jQuery112305375218782156135_&lmt=0&klt' \
    #       '=101&fields1=f1&fields2=f51&ut=b2884a393a59ad64002292a3e90d46a5&secid=1.000001&secid2=0.399001'
    url = 'http://push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery351020143393668275844_1674273725401&secid=0' \
          '.000001&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1&fields2=f51&klt=101&fqt=1&end=20500101&lmt=1000000' \
          '&_=1674273725455 '

    headers = {
        'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 5.1.1; TAS-AN00 Build/TAS-AN00)'}

    session = requests.Session()
    # 禁用安全请求警告
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    # dicts = {}
    try:
        html = session.get(url=url, data=data, headers=headers, verify=False, timeout=timeout).text
        _str = re.search('\{(\S*\s*)*\}', html).group()
        html = json.loads(_str)
    except Exception as spider_error:
        raise ValueError(spider_error)
    return html['data']['klines']


if __name__ == "__main__":
    # print(get_min_line_data_eastmoney('0.002186'))
    get_history_date_eastmoney()
